package com.biubiushop.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSONObject;
import com.biubiushop.constant.ShouQianbaConstant;
import com.biubiushop.exception.ServiceException;
import com.biubiushop.service.IOrdersService;
import com.biubiushop.util.shouqianba.ShouQianBaUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author chenjia
 * @date 2019/06/24
 */
@RestController
@RequestMapping("/shareTissue")
@Api(tags = "订单模块")
public class OrderController {
    private static final Log log = LogFactory.get();

    @Resource
    IOrdersService orderService;

    @ApiOperation(value = "用户获取所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "token", value = "用户token", required = true)
    })
    @GetMapping("/order/v1/getOrderList")
    public Serializable getOrderList(String userId) throws Exception {
        if (StrUtil.isEmpty(userId)) {
            throw new ServiceException("用户为空");
        }
        return (Serializable) orderService.getOrderList(userId);
    }

    @ApiOperation(value = "用户创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "equipmentId", value = "设备id", required = true),
            @ApiImplicitParam(name = "commodityId", value = "商品id", required = true),
            @ApiImplicitParam(name = "buyLength", value = "购买总长度", required = true),
            @ApiImplicitParam(name = "amoutMoney", value = "总价", required = true),
            @ApiImplicitParam(name = "payway", value = "支付方式(1-支付宝,3-微信,6-QQ)", required = true)
    })
    @PostMapping("/order/v1/createOrder")
    public Serializable createOrder(String equipmentId, String commodityId, BigDecimal amoutMoney, Integer buyLength, String payway)
            throws Exception {
        if (StrUtil.hasEmpty(equipmentId, commodityId, payway)) {
            throw new ServiceException("参数缺失");
        }

        return orderService.createOrder(equipmentId, commodityId, amoutMoney, buyLength, payway);
    }

    @ApiOperation(value = "查询收钱吧订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminal_sn", value = "收钱吧终端ID", required = true),
            @ApiImplicitParam(name = "sn", value = "收钱吧系统唯一订单号", required = true),
            @ApiImplicitParam(name = "client_sn", value = "商户自己订单号", required = true),
    })
    @PostMapping("/order/v1/queryOrderStatus")
    public Serializable queryOrderStatus(String terminal_sn, String sn, String client_sn) {
        if (StrUtil.hasEmpty(terminal_sn, sn, client_sn)) {
            throw new ServiceException("参数缺失");
        }
        JSONObject params = new JSONObject();
        params.put("terminal_sn", terminal_sn);
        params.put("sn", sn);
        params.put("client_sn", client_sn);
        String result = ShouQianBaUtil.query(params, ShouQianbaConstant.terminal_key);
        log.info(result);
        return result;
    }

    @ApiOperation(value = "撤销收钱吧订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminal_sn", value = "收钱吧终端ID", required = true),
            @ApiImplicitParam(name = "sn", value = "收钱吧系统唯一订单号", required = true),
            @ApiImplicitParam(name = "client_sn", value = "商户自己订单号", required = true),
    })
    @PostMapping("/order/v1/cancelOrder")
    public Serializable cancelOrder(String terminal_sn, String sn, String client_sn) {
        if (StrUtil.hasEmpty(terminal_sn, sn, client_sn)) {
            throw new ServiceException("参数缺失");
        }
        JSONObject params = new JSONObject();
        params.put("terminal_sn", terminal_sn);
        params.put("sn", sn);
        params.put("client_sn", client_sn);
        String result = ShouQianBaUtil.cancel(params, ShouQianbaConstant.terminal_key);
        return result;
    }

    @ApiOperation(value = "收钱吧订单退款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "terminal_sn", value = "收钱吧终端ID", required = true),
            @ApiImplicitParam(name = "sn", value = "收钱吧系统唯一订单号", required = true),
            @ApiImplicitParam(name = "client_sn", value = "商户自己订单号", required = true),
    })
    @PostMapping("/order/v1/refundOrder")
    public Serializable refundOrder(String terminal_sn, String sn, String client_sn) {
        if (StrUtil.hasEmpty(terminal_sn, sn, client_sn)) {
            throw new ServiceException("参数缺失");
        }
        JSONObject params = new JSONObject();
        params.put("terminal_sn", terminal_sn);
        params.put("sn", sn);
        params.put("client_sn", client_sn);
        return ShouQianBaUtil.cancel(params, ShouQianbaConstant.terminal_key);
    }


    @ApiOperation(value = "支付成功后出纸")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "equipmentId", value = "设备id", required = true),
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true),
    })
    @PostMapping("/order/v1/execute")
    public void execute(String equipmentId, String orderId)
            throws Exception {
        if (StrUtil.hasEmpty(equipmentId, orderId)) {
            throw new ServiceException("参数缺失");
        }

        orderService.execute(equipmentId, orderId);
    }

    @ApiOperation(value = "用户发起微信小程序支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true),
            @ApiImplicitParam(name = "token", value = "用户token", required = true),
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true),
            @ApiImplicitParam(name = "amoutMoney", value = "总金额", required = true)
    })
    @PostMapping("/order/v1/startWxMiniPay")
    public Serializable startWxMiniPay(String orderId, String amoutMoney)
            throws Exception {
        if (StrUtil.hasEmpty(orderId, amoutMoney)) {
            throw new ServiceException("参数缺失");
        }
        orderService.startWxMiniPay(orderId, amoutMoney);
        return null;
    }

    @ApiOperation("app发起支付宝支付")
    @PostMapping("/order/v1/startALiPay")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true)
    public Serializable startALiPay(@RequestBody String orderId)
            throws Exception {
        if (StrUtil.hasEmpty(orderId)) {
            throw new ServiceException("参数缺失");
        }
        String totalAmount = "0.01";
        return orderService.startALiPay(orderId, totalAmount);
    }

    @ApiOperation("app发起支付宝转账")
    @PostMapping("/order/v1/startAppTrans")
    @ApiImplicitParam(name = "outBizNo", value = "系统内转账唯一标识", required = true)
    public Serializable startAppTrans(String outBizNo)
            throws Exception {
        if (StrUtil.hasEmpty(outBizNo)) {
            throw new ServiceException("参数缺失");
        }
        orderService.startAppTrans(outBizNo);
        return null;
    }

    @ApiOperation("微信小程序支付回调地址")
    @PostMapping("/order/v1/wxMiniNotify")
    public Serializable wxMiniNotify(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        orderService.wxMiniNotify(request, response);
        return null;
    }

    @ApiOperation("支付宝app回调地址")
    @PostMapping("/order/v1/aLiNotify")
    public Serializable aLiNotify(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        orderService.aLiNotify(request, response);
        return null;
    }

}
